package com.yang.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.yang.domain.entity.ShopType;
import com.yang.mapper.ShopTypeMapper;
import com.yang.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.yang.constants.RedisConstants.CACHE_SHOP_TTL;
import static com.yang.constants.RedisConstants.CACHE_TYPE_LIST;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yang
 * @since 2022-10-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ShopTypeMapper shopTypeMapper;

    @Override
    public List<ShopType> queryTypeList() {

        String shopTypeJson = stringRedisTemplate.opsForValue().get(CACHE_TYPE_LIST);

        if (shopTypeJson == null) {

            List<ShopType> shopTypes = shopTypeMapper.findAllShopType();

            stringRedisTemplate.opsForValue().set(CACHE_TYPE_LIST, JSON.toJSONString(shopTypes), CACHE_SHOP_TTL, TimeUnit.MINUTES);

            return shopTypes;
        }

        return JSONUtil.toList(shopTypeJson, ShopType.class);

    }

}
